package com.jdplat.jdm.core.web.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by qirp on 2016/11/15.
 */
public class BaseDao<T, PK extends Serializable> implements IBaseDao<T, PK> {
	
	@Autowired
	protected SqlSessionTemplate sqlSessionTemplate;
	
	public List queryForList(String s, Object obj) {
		return sqlSessionTemplate.selectList(s, obj);
	}

	public void insert(String s, Object obj) {
		sqlSessionTemplate.insert(s, obj);
	}

	public void insert(String s) {
		sqlSessionTemplate.insert(s);
	}

	public List queryForList(String s) {
		return this.sqlSessionTemplate.selectList(s);
	}
	
	public List queryForPage(String s,Map map) throws SQLException{
		int pages = Integer.parseInt(map.get("pages").toString());
		int size = Integer.parseInt(map.get("size").toString());
        
        int iOffset = (pages - 1) * size;    
        RowBounds objRowBounds = new RowBounds(iOffset, size);
        return this.sqlSessionTemplate.selectList(s, map, objRowBounds);
	}

	public int update(String s, Object obj) {
		return sqlSessionTemplate.update(s, obj);
	}

	public int update(String s) {
		return sqlSessionTemplate.update(s);
	}

	public int delete(String s, Object obj) {
		return sqlSessionTemplate.delete(s, obj);
	}

	public int delete(String s) {
		return sqlSessionTemplate.delete(s);
	}

	public Connection getConnection() throws SQLException {
		return sqlSessionTemplate.getConnection();
	}

}
